package io.dbw.modules.business.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.dbw.common.utils.*;
import io.dbw.modules.business.entity.SysBorrowEntity;
import io.dbw.modules.business.service.SysBooksService;
import io.dbw.modules.business.service.SysBorrowService;
import io.dbw.modules.business.vo.HomeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 *
 * @author lxc
 * @date 2024-03-30 16:55:49
 */
@Validated
@RestController
@RequestMapping("sys/borrow")
@Api(tags = "借书管理")
public class SysBorrowController extends AbstractController {
    @Autowired
    private SysBorrowService sysBorrowService;
    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("借书")
    @RequiresPermissions("manage:borrow:save")
    public Result save(@Validated @RequestBody SysBorrowEntity sysBorrow){
        Long userId = ShiroUtils.getUserId();
        sysBorrow.setCreateBy(userId);
        sysBorrow.setUpdateBy(userId);
        sysBorrow.setId(null);
        sysBorrow.setStillFlag(Constant.SYSTEM_COMMON_N);
        long count = sysBorrowService.count(new QueryWrapper<SysBorrowEntity>()
                .eq("book_id", sysBorrow.getBookId())
                .eq("user_id", sysBorrow.getUserId())
                .eq("still_flag", Constant.SYSTEM_COMMON_N));
        if(count>0){
            return Result.error("您已借该书,不能重复借");
        }
        sysBorrowService.saveSysBorrow(sysBorrow);
        return writeSuccess();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value="还书")
    @RequiresPermissions("manage:borrow:update")
    public Result update(@Validated @RequestBody SysBorrowEntity sysBorrow){
        long count = sysBorrowService.count(new QueryWrapper<SysBorrowEntity>()
                .eq("book_id", sysBorrow.getBookId())
                .eq("user_id", sysBorrow.getUserId())
                .eq("still_flag", Constant.SYSTEM_COMMON_N));
        if(count==0){
            return Result.error("您未借该书,请联系管理员");
        }
        sysBorrowService.updateSysBorrow(sysBorrow);
        return writeSuccess();
    }




}
